<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                v-model="filterField.projectName"
                placeholder="请输入项目名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="申请状态" class="row-padding-bottom">
              <el-select
                v-model="filterField.status"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.text"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="申请日期" class="row-padding-bottom">
                <timeSelection
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
          </template>
          <el-form-item>
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          @sort-change="sortChange"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column label="申请单位" width="230" :formatter="formatTd">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.companyName"
                :columnName="'companyName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column label="项目名称" width="230" :formatter="formatTd">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            prop="returnMoney"
            width="180"
            label="退还金额(元)"
            sortable="custom"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            width="180"
            label="申报时间"
            sortable="custom"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            width="120"
            label="状态"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column label="操作" width="220" align="left" fixed="right">
            <template slot-scope="scope">
              <template>
                <el-link
                  v-hasBtn="'refundDeposit:update'"
                  v-if="scope.row.status === 'WAIT_AUDIT'"
                  @click="showDetail(scope.row, 'edit')"
                  type="primary"
                >
                  查看详情
                </el-link>
                <el-link
                  v-hasBtn="'refundDeposit:detail'"
                  v-else
                  @click="showDetail(scope.row, 'detail')"
                  type="primary"
                >
                  查看详情
                </el-link>
              </template>
              <el-link
                v-hasBtn="'refundDeposit:update'"
                v-if="scope.row.status === 'REJECTED'"
                @click="showDetail(scope.row, 'edit')"
                type="primary"
              >
                修改审核结果
              </el-link>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
    <!-- 审核 -->
    <el-dialog
      title="审核退还保证金"
      :visible.sync="examineDialog"
      @close="closeDialog"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      width="900px"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-row>
          <!-- <el-col :span="24">
                        <el-form-item label="审核状态：">
                            <p>{{depositDetail.statusDesc||'--'}}</p>
                        </el-form-item>
                    </el-col>
                    <template v-if="depositDetail.status==='PASSED'||depositDetail.status==='REJECTED'">
                        <el-col :span="8">
                            <el-form-item label="审核人：">
                                <p>{{depositDetail.checkName||'--'}}</p>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="审核时间：">
                                <p>{{depositDetail.reviewTime||'--'}}</p>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" v-if="depositDetail.status==='REJECTED'">
                            <el-form-item label="不通过原因：" label-width="120px">
                                <p>{{depositDetail.description||'--'}}</p>
                            </el-form-item>
                        </el-col>
                    </template> -->
          <detailHeader
            :status="
              depositDetail.status === 'WAIT_AUDIT'
                ? 1
                : depositDetail.status === 'PASSED'
                ? 2
                : 3
            "
            :infoList="infoList"
            :mark="'mark'"
            :statusDesc="depositDetail.statusDesc"
          ></detailHeader>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >申请单位：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.companyName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >项目名称：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.projectName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >退还金额：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.returnMoney || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >退还银行：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.bankName || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >退还账号：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.account || '--'
            }}</span>
          </el-col>
          <el-col :span="12" class="labelWidthSty detailInfoItemSty">
            <span class="gridContent color333 fontSizeF fontWeight"
              >申报时间：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">{{
              depositDetail.createTime || '--'
            }}</span>
          </el-col>
          <el-col :span="24" class="labelWidthSty detailInfoItemSty mrB30">
            <span class="gridContent color333 fontSizeF fontWeight"
              >附件：</span
            >
            <span class="gridContent empContentSty color666 fontSizeF">
              <template
                v-if="
                  depositDetail.fileList && depositDetail.fileList.length > 0
                "
              >
                <span>已上传</span>
                <el-link
                  @click="showAttach(depositDetail.fileList)"
                  type="primary"
                  style="
                    vertical-align: text-bottom;
                    line-height: 15px !important;
                  "
                  >查看</el-link
                >
              </template>
              <template v-else>
                <span>未上传</span>
              </template>
            </span>
          </el-col>
        </el-row>
        <el-divider v-if="type === 'edit'"></el-divider>
        <div
          class="secondaryTitle secondaryTitleTheme mrB18 mrT24"
          v-if="type === 'edit'"
        >
          审核
        </div>
        <tempalte
          v-if="
            depositDetail.status === 'WAIT_AUDIT' ||
            (depositDetail.status === 'REJECTED' && type === 'edit')
          "
        >
          <el-form-item label="审核处理：">
            <el-radio-group v-model="formData.status">
              <el-radio label="PASSED">审核通过</el-radio>
              <el-radio label="REJECTED">驳回</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="驳回原因："
            prop="description"
            v-if="formData.status === 'REJECTED'"
          >
            <el-input
              v-model="formData.description"
              type="textarea"
              maxlength="300"
              show-word-limit
              placeholder="请输入驳回原因"
              clearable
              class="dialoglargeInp"
            ></el-input>
          </el-form-item>
        </tempalte>
      </el-form>
      <div div slot="footer" v-if="type === 'edit'" class="dialog-footer">
        <el-button @click.native="closeDialog" class="cancel">取消</el-button>
        <el-button
          type="primary"
          @click.native="submitForm"
          v-sdc
          :loading="isLoading"
          class="save"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <!-- 附件 -->
    <contractEnclosureDialog
      v-if="uploadContract"
      dialogTitle="竣工报告"
      :tableData="fileBackList"
      @closeDialog="hiddenDialog"
    ></contractEnclosureDialog>
  </div>
</template>
<script>
import { mapState } from 'vuex'
import scrollTable from '@/mixin/scrollTable.js'
import detailHeader from '@/components/publicCom/detailHeader.vue'

export default {
  name: 'refundDeposit',
  mixins: [scrollTable],
  components: { detailHeader },
  data() {
    return {
      titleText: '保证金退还',
      statusOptions: [
        {
          text: '待审核',
          id: 'WAIT_AUDIT',
        },
        {
          text: '已通过',
          id: 'PASSED',
        },
        {
          text: '已驳回',
          id: 'REJECTED',
        },
      ],
      tableList: [],
      requestUrl: 'api-d/depositreturn/expand/list',
      //搜索条件
      filterField: {
        projectName: '',
        status: '',
        startTime: '',
        endTime: '',
      },
      pageSize: 20,
      pageNo: 1,
      loading: false,
      isShow: false,
      depositDetail: {},
      examineDialog: false,
      type: '',
      formData: {
        id: '',
        status: 'PASSED',
        description: '',
      },
      rules: {
        description: [
          {
            required: true,
            message: '请输入驳回原因',
            trigger: 'blur',
          },
        ],
      },
      isLoading: false,
      fileBackList: [],
      uploadContract: false,
      advanced: false,
    }
  },
  computed: {
    ...mapState(['areasOptions', 'industryOptions']),
    infoList() {
      if (this.depositDetail.status == 'REJECTED') {
        return [
          { title: '审核人', value: this.depositDetail.checkName },
          { title: '审核时间', value: this.depositDetail.reviewTime },
          { title: '不通过原因', value: this.depositDetail.description },
        ]
      } else if (this.depositDetail.status == 'PASSED') {
        return [
          { title: '审核人', value: this.depositDetail.checkName },
          { title: '审核时间', value: this.depositDetail.reviewTime },
        ]
      }
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.pagination.getTableListData()
    })
  },
  created() {
    this.filterField.projectName = this.$route.query.projectName
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 列表
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    searchList() {
      this.$refs.pagination.getTableListData()
    },
    async showDetail(row, type) {
      this.type = type
      this.depositDetail = row
      this.getDepositDetail(row.id)
      this.formData.id = row.id
      this.formData.status = row.status === 'REJECTED' ? row.status : 'PASSED'
      this.formData.description =
        row.status === 'REJECTED' ? row.description : ''
      this.examineDialog = true
    },
    // 查询详情
    getDepositDetail(id) {
      return this.axios({
        method: 'post',
        url: 'api-d/depositreturn/expand/detail',
        data: { id },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.depositDetail.fileList = response.data.result.fileList
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    closeDialog() {
      this.$refs['ruleForm'].resetFields()
      this.examineDialog = false
    },
    submitForm() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true
          const { id, status, description } = this.formData
          let params = { id, status, description }
          this.axios
            .post('api-d/depositreturn/expand/approval', params)
            .then((res) => {
              if (!res.data.hasError) {
                this.$message.success('保存成功')
                this.isLoading = false
                this.formData.id = ''
                this.formData.status = 'PASSED'
                this.formData.description = ''
                this.examineDialog = false
                this.$refs.pagination.getTableListData(
                  this.pageNo,
                  this.pageSize
                )
              } else {
                this.$message({
                  type: 'warning',
                  message: res.data.errorMsg,
                })
                this.isLoading = false
              }
            })
        } else {
          return false
        }
      })
    },
    showAttach(files) {
      this.fileBackList = files
      this.uploadContract = true
    },
    hiddenDialog() {
      this.uploadContract = false
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.startTime = val
      } else {
        this.filterField.startTime = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.endTime = val
      } else {
        this.filterField.endTime = ''
      }
    },
  },
}
</script>
<style lang="less" scoped>
.mainCon {
  top: 197px;
}
</style>
